#!/bin/bash

# Transform the moving image for all resolutions
#

#############################################################################
functionname=`basename "$0"`
function PrintHelp()
{
  echo "Transform the moving image for all resolutions"
  echo
  echo "Usage:"
	echo $functionname
	echo "  -d      directory"
	echo "  -i      input file"
	echo "  [-E]    elastix level, default 0"
	echo "  [-R]    resolution level, default all"
	echo "  [-t]    number of threads, default all"
}

#############################################################################

# Check for PrintHelp
if [[ $1 == "--help" ]]
then
	PrintHelp
	exit 64
fi

# Check for the number of arguments
if [ $# -lt 2 ] || [ $# -gt 12 ]
then
  echo "ERROR: not enough arguments!"
  echo "Call \""$functionname" --help\" for help."
  exit 64
fi

# Default values.
tpdir=""
elastixlevel=0
resolution=99
in=""
threads="99"

# Get the command line arguments.
while getopts "d:i:E:R:t:" argje
do
  case $argje in 
    d) tpdir="$OPTARG";;
    i) in="$OPTARG";;
    E) elastixlevel="$OPTARG";;
    R) resolution="$OPTARG";;
    t) threads="$OPTARG";;
    *) echo "ERROR: Wrong arguments!"; exit 65;;
  esac
done

# Some shortcuts.
E="$elastixlevel"
R="$resolution"

# Check the command line arguments.
if [[ "$tpdir" == "" ]]
then
  #echo "ERROR: The directory \"$tpdir\" does not exist."
  echo "ERROR: You should supply \"-d\"."
  exit 66
fi

if [[ "$in" == "" ]]
then
  #echo "ERROR: The directory \"$tpdir\" does not exist."
  echo "ERROR: You should supply \"-i\"."
  exit 66
fi

if [ ! -e "$in" ]
then
	echo "ERROR: The file \"$in\" does not exist. Quitting "$functionname"."
  exit 66
fi

#############################################################################

# Check which resolution levels are required.
if [ "$R" -eq "99" ]
then
	i=0; Rarray="";
	while [ 1 ]
	do
		file=$tpdir"/TransformParameters."$E".R"$i".txt"
		if [ -e $file ]
		then
			Rarray=$Rarray" "$i
			let "i+=1"
		else
			break
		fi
	done
else
	Rarray=$R
fi

if [[ "$Rarray" == "" ]]
then
	echo "ERROR: no (finished) resolution information."
	exit 65;
fi

#############################################################################

# Transform final result
echo -n $E" "
tpfile=$tpdir"/TransformParameters."$E".txt"
transformix -in $in -tp $tpfile -out $tpdir -threads $threads > /dev/null

# Check for success
if [[ $? -ne 0 ]]
then
	echo "ERROR: \"transformix -in "$in" -tp "$tpfile" -out "$tpdir"\" failed."
	exit 1
fi

# Rename
outfile=$tpdir"/result."$E".mhd"
pxcastconvert -in $tpdir"/result.mhd" -out $outfile > /dev/null

# Loop over all resolutions.
for R in $Rarray
do
	# Inform the user
	echo -n $E".R"$R" "

	# Deform input image.
	tpfile=$tpdir"/TransformParameters."$E".R"$R".txt"
	transformix -in $in -tp $tpfile -out $tpdir -threads $threads > /dev/null
	# Check for success
	if [[ $? -ne 0 ]]
	then
		echo "ERROR: \"transformix -in "$in" -tp "$tpfile" -out "$tpdir"\" failed."
		exit 1
	fi

	# Rename
	outfile=$tpdir"/result."$E".R"$R".mhd"
	pxcastconvert -in $tpdir"/result.mhd" -out $outfile > /dev/null

done
echo

rm $tpdir"/result.mhd" $tpdir"/result.raw"

# Exit function, return success
exit 0

